package com.ccl.iot.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ccl.iot.R;
import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.ir.DBUtils;
import com.hzy.tvmao.KKACManagerV2;
import com.hzy.tvmao.utils.DataStoreUtil;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 创建者     王浩
 * 创建时间   2017/5/23 15:19
 * 描述	      ${TODO}
 */

public class SubBlirMiniView extends RelativeLayout {
    public static int STATE_NORMAL = 0;
    public static int STATE_DELETE = 1;

    private ImageView mIvBlir, mIvBlirDelete;
    private MarqueeLabel mMqlName;
    private IOTSubBLir mSubBLir;
    private CheckBox mCheckBox;
    private boolean mSelectable;

    public SubBlirMiniView(Context context, boolean selectable) {
        super(context);
        mSelectable = selectable;
        init();
    }

    private IOTSubBLir.OnCheckedChangeListener mOnCheckedChangeListener = new IOTSubBLir.OnCheckedChangeListener() {
        @Override
        public void onCheckedChange() {
            CodeviewBean batchCodes = mSubBLir.getBatchCodes();

            mCheckBox.setChecked(batchCodes != null);
        }
    };

    private String intToHex(int aValue) {
        String tHex = Integer.toHexString(aValue);

        if (tHex.length() < 8) {
            char tZero[] = new char[8 - tHex.length()];

            Arrays.fill(tZero, '0');
            return new String(tZero) + tHex;
        } else {
            return tHex;
        }
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_blirdev, this);
        mIvBlir = (ImageView) findViewById(R.id.iv_blirdev);
        mIvBlirDelete = (ImageView) findViewById(R.id.iv_delete_blirdev);
        mMqlName = (MarqueeLabel) findViewById(R.id.mql_blirdev_name);
        mCheckBox = (CheckBox) findViewById(R.id.cb_batch_selet);
        if (mSelectable) {
            mCheckBox.setVisibility(View.VISIBLE);
        }

        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mSubBLir != null) {
                    if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(mSubBLir.getDevType())) {
                        CodeviewBean batchCodes = mSubBLir.getBatchCodes();
                        if (isChecked && (batchCodes == null || !DBUtils.CODE_TYPE_STATUS.equals(batchCodes.KeyType))) {
                            HashMap<String, CodeviewBean> irCode = mSubBLir.getIRCode();
                            CodeviewBean api = irCode.get(DBUtils.CODE_TYPE_API);
                            if (api != null && api.ac != null) {
                                KKACManagerV2 KKACManager = new KKACManagerV2();
                                KKACManager.initIRData(api.ac.rid, api.ac.exts, null);
                                String acState = DataStoreUtil.i().getString(mSubBLir.getUDID(), "");//获取以前保存过的空调状态
                                KKACManager.setACStateV2FromString(acState);

                                int[] patternsInArray = KKACManager.getACIRPatternIntArray();//这些码可以直接给ConsumerIR发送出去
                                if (patternsInArray != null && patternsInArray.length > 0) {
                                    String code = "";
                                    int length = patternsInArray.length;
                                    for (int i = 0; i < length; i++) {
                                        if (i < length - 1) {
                                            code += patternsInArray[i] + ",";
                                        } else {
                                            code += patternsInArray[i];
                                        }
                                    }

                                    Log.e("IRPattern", code);

                                    if (!TextUtils.isEmpty(code)) {
                                        int mode = KKACManager.getCurModelType();
                                        int power = KKACManager.getPowerState();
                                        int tmp = KKACManager.getCurTemp();
                                        int speed = KKACManager.getCurWindSpeed();
                                        int dir = KKACManager.getCurUDDirect();

                                        Log.e("Code", "mode: "+mode);
                                        Log.e("Code", "power: "+power);
                                        Log.e("Code", "tmp: "+tmp);
                                        Log.e("Code", "speed: "+speed);
                                        Log.e("Code", "dir: "+dir);
                                        Log.e("Code", "code: "+code);
                                        String blirCode = intToHex(mode) + intToHex(power) + intToHex(tmp) +
                                                intToHex(speed) + intToHex(dir) + BlirConst.kk2bl(code);
                                        Log.e("Code", "blirCode: "+blirCode);
                                        CodeviewBean acCodeBean = new CodeviewBean(mSubBLir.getDbId(), (int) mSubBLir.getSubId()
                                                , mSubBLir.getDevType(), DBUtils.CODE_TYPE_STATUS, DBUtils.CODE_TYPE_STATUS,
                                                DBUtils.CODE_TYPE_STATUS, blirCode, 0);
                                        mSubBLir.setBatchCodes(acCodeBean);
                                    }
                                }
                            }
                        } else {
                            if (!isChecked) {
                                mSubBLir.setBatchCodes("");
                            }
                        }
                    } else {
                        if (isChecked && mSubBLir.getBatchCodes() == null) {
                            mCheckBox.setChecked(false);
                        } else {
                            if (!isChecked) {
                                mSubBLir.setBatchCodes("");
                            }
                        }
                    }
                }
            }
        });
    }

    public void setSubDev(IOTSubBLir dev) {
        if (dev != null) {
            if (mSelectable) {
                if (mSubBLir != null) {
                    mSubBLir.setOnCheckedChangeListener(null);
                }
                dev.setOnCheckedChangeListener(mOnCheckedChangeListener);
            }
            mIvBlirDelete.setTag(dev);
            mIvBlir.setTag(dev);
            mIvBlir.setImageResource(DeviceIconProvider.getBlirSelectorId(dev.getDevType()));
            mSubBLir = dev;
            mMqlName.setText(dev.getName());

            CodeviewBean batchCodes = mSubBLir.getBatchCodes();
            mCheckBox.setChecked(batchCodes != null);
        }
    }

    public ImageView getIvBlirView() {
        return mIvBlir;
    }

    public IOTSubBLir getSubBLir() {
        return mSubBLir;
    }

    public void setOnBlirClickListener(OnClickListener l, OnLongClickListener ll) {
        if (l != null) {
            mIvBlir.setOnClickListener(l);
            mIvBlirDelete.setOnClickListener(l);
        }

        if (ll != null) {
            mIvBlir.setOnLongClickListener(ll);
        }
    }

    public void updateView(int state) {
        if (state == STATE_NORMAL) {
            mIvBlirDelete.setVisibility(View.GONE);
        } else if (state == STATE_DELETE) {
            mIvBlirDelete.setVisibility(View.VISIBLE);
        }
    }
}
